# -*- coding:utf-8 -*-
'''
将一个字符串转换成一个整数，要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0

解题思路：判断符号位正负，然后判断是否存在整数值外的数，然后利用ascii值做差
'''
class Solution:
    def StrToInt(self, s):
        if not s: return 0
        nums = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
        sign = 1
        res = 0
        for x in s:
            if x == '-' or x == '+': continue
            if x not in nums:return 0
            res = res * 10 + (ord(x) - ord('0'))
        if s[0] == '-': sign = -1
        res = res * sign
        return res

s = Solution()
print(s.StrToInt("1-23"))